package org.dairy.farms.nandana.web.controller;

import org.dairy.farms.model.Person;
import org.dairy.farms.nandana.core.logic.LivestockManagementLogic;
import org.dairy.farms.nandana.core.logic.PersonManagementLogic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: GiriD
 * Date: 12 Sep, 2011
 * Time: 2:30:28 PM
 */
@Controller
public class PersonController {
    private static final Logger logger = LoggerFactory.getLogger(PersonController.class);
    @Autowired
    private PersonManagementLogic personManagementLogic;

    @RequestMapping(value="person/findByRoleType.json",method = RequestMethod.GET)
    public void findByRoleTypes(@RequestParam(value="id",required = true)Long livestockId, Model model,
                                @RequestParam(value="roleTypes", required = true) String roleTypesCsv){
        String[] roleTypes = roleTypesCsv.split(",");
        List<Person> personsList = personManagementLogic.findByPersonRole(roleTypes,livestockId);
        model.addAttribute("persons",personsList);
        model.addAttribute("total",personsList.size());
    }

    @RequestMapping(value="person/searchPerson.json", method=RequestMethod.GET)
    public void searchPerson(@RequestParam(value = "searchBy", required = true) String searchBy,
                                @RequestParam(value = "value", required = true) String searchParam,
                                @RequestParam(value = "page", required = true) int page,
                                @RequestParam(value = "start", required = true) int start,
                                @RequestParam(value = "limit", required = true) int limit,
                                Model model){
        List<Person> personList = personManagementLogic.searchPersonBy(searchBy,searchParam,page,start,limit);
        model.addAttribute("personList", personList);
        model.addAttribute("total", personList.size());
    }
}
